package com.emser.unc.server;

import java.util.ArrayList;
import java.util.Date;

import com.emser.unc.client.service.ProductService;
import com.emser.unc.serializable.ProductDTO;
import com.emser.unc.serializable.ResultDTO;
import com.emser.unc.shared.exception.NotLoggedInException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ProductServiceImpl extends RemoteServiceServlet implements ProductService {
	private static final long serialVersionUID = 1L;
	
	
	@Override
	public boolean addProduct(String userAName, ProductDTO productDTO)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else return AppLibProduct.addProductDataStore(userAName, productDTO);
	}

	@Override
	public ResultDTO<ProductDTO> getProducts(String cursorString, int range,String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else return  AppLibProduct.getProductsDataStore(cursorString,range,userAName);
	}

	@Override
	public boolean deleteProducts(String userAName, ArrayList<Long> codeProducts)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else return AppLibProduct.deleteProductsDataStore(userAName, codeProducts);
	}

	@Override
	public boolean updateProduct(String userAName, ProductDTO product) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else {
			return AppLibProduct.updateProductDataStore(userAName,product);}
	}
	@Override
	public void addProductsSellerList(String userAName,ArrayList<Long> codeProducts,String sellerKey)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else AppLibProduct.addProductsSeller(codeProducts, sellerKey,userAName);
	}
	
	@Override
	public ResultDTO<ProductDTO> getProductsSeller(String cursorString, int range,String userAName, String sellerKey) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}
		else {
			ResultDTO<ProductDTO> resultData = AppLibProduct.getProductsDataStore(cursorString,range, userAName);
			ResultDTO<ProductDTO> pseller= new ResultDTO<ProductDTO>();
			pseller.setCursorString(resultData.getCursorString());
			pseller.setList(resultData.getList());
			pseller.setSelectedIDs((ArrayList<Long>) AppLibProduct.getProductsCode(sellerKey));
			return pseller;
		}
	}
	
	@Override
	public Integer getProductsCount(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else return AppLibProduct.getProductsCount(userAName);
	}

	@Override
	public ResultDTO<ProductDTO> getSearchProducts(String cursorString, int range,
			ArrayList<String> searchParam) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else return AppLibProduct.getSearchProducts(cursorString,range,searchParam);
	}

	@Override
	public Integer getSearchedProductsCount(ArrayList<String> searchParam)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else
			return AppLibProduct.getSearchedProductsCount(searchParam);
	}

	@Override
	public Date updateStocks(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else{
			Date date = new Date();
			boolean flag= AppLibProduct.updateStock(userAName, date);
			if(flag)
				return date;
			else
				return null;
		}
	}

	@Override
	public Date getLastUpateDate(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest()))
			throw new NotLoggedInException("Not logged in.");
		else{
		  return AppLibProduct.getLastUpdateDate(userAName);
		}
	}
}
